/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pio.action.administrador;

import DBMS.DataBaseManager;
import clases.ReportePreseleccion;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

/**
 *
 * @author charles
 */
public class GenerarReportePreseleccion extends DispatchAction {
    
    public ActionForward page(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        return mapping.findForward("page");
    }

    public ActionForward make(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        DataBaseManager d =  DataBaseManager.getInstance();
        ReportePreseleccion reporte = (ReportePreseleccion) form;
        reporte.setPreselectos(d.consultarPreselectos());
        reporte.setNumPreselectos(reporte.getPreselectos().size()/3);
        reporte.setNumAspirantes(d.numeroAspirantes());
        reporte.setNumNoPreselectos(reporte.getNumAspirantes()-reporte.getNumPreselectos());
        List l = d.consultarPreselectosPorInstitucion();
        List l2 = d.consultarNoPreselectosPorInstitucion();
        int numInst[] = new int[l.size()];
        String nombreInst[] = new String[l.size()];
        Iterator i = l.iterator();
        int n = 0;
        while (i.hasNext()) {
            ArrayList h = (ArrayList)i.next();
            nombreInst[n] = (String)h.get(0);
            numInst[n] = ((Integer)h.get(1)).intValue();
            n++;
        }
        int numInst2[] = new int[l.size()];
        i = l2.iterator();
        n = 0;
        while (i.hasNext()) {
            ArrayList h = (ArrayList)i.next();
            numInst2[n] = ((Integer)h.get(1)).intValue();
            n++;
        }
        reporte.setNumInst(numInst);
        reporte.setNombreInst(nombreInst);
        reporte.setNumInst2(numInst2);
        String path = reporte.getPath();
        try{
            File f = new File(path);
            int k = 1;
            while (f.exists()){
                path = reporte.getPath();
                path= path + "("+k+")";
                f = new File(path);
                k++;                
            }   
            FileOutputStream file = new FileOutputStream(f);
           // reporte.generar(file);
            reporte.setMensaje("PDF creado en /home/" + path);
            reporte.setPath("");
            return mapping.findForward("success");
        } catch(Exception e){
            reporte.setMensaje("Nombre invalido");
            return mapping.findForward("failure");
        }
    }
}
